<?php
namespace Sys\Controller;
use Todo\Controller\ApiController;
use Todo\Service\DictService;

class DictController extends ApiController {


    protected function _filter(&$where){
        $where['is_del'] = 0;
        $keyword = I('get.keyword/s');
        if($keyword) $where['_string'] = "title like '%" . $keyword . "%' OR code like '%" . $keyword . "%'";
    }
    
    public function validateCode(){
        $value = I('get.code');
        if(!$value) die('false');

        $this->_validate('code', $value, 'unique');
    }

    public function info(){
        $code = I('get.code');
        if(!$code) $this->apiError('参数不完整');

        $data = DictService::getInstance()->getDict($code);
        if(!$data) $this->apiError('数据不存在');

        $map = array();
        foreach ($data['map'] as $k => $v){
            $map[] = array(
                'id' => $k,
                'title' => $v
            );
        }
        $this->apiSuccess($map);
    }
}